package com.quectel.business.dms;

import cn.hutool.core.util.ClassUtil;
import com.quectel.business.dms.application.access.AbstractApplicationAccessor;
import com.quectel.business.dms.application.access.Accessor;
import com.quectel.business.dms.application.access.accessor.ieg.AbstractIntelligentEntranceGuardAccessor;
import com.quectel.business.dms.application.access.accessor.im.AbstractIntelligentMonitorAccessor;
import com.quectel.business.dms.application.access.accessor.ipg.AbstractIntelligentParkingGateAccessor;
import com.quectel.core.module.device.dto.DeviceTypeDto;
import com.quectel.util.ex.CheckErrorException;
import com.quectel.util.spring.SpringUtils;
import lombok.extern.slf4j.Slf4j;

import java.util.Set;

/**
 * @author caolu
 * @email louis.cao@quectel.com
 */
@Slf4j
public class AccessorUtils {

    private static Set<Class<?>> typeAccessorClasses = null;

    static {
        typeAccessorClasses = ClassUtil.scanPackageBySuper("com.quectel.business.dms.application.access.accessor", AbstractApplicationAccessor.class);
    }

    /**
     * 得到智慧道闸的相关能力
     *
     * @param deviceTypeDto
     * @return
     */
    public static AbstractIntelligentParkingGateAccessor getIntelligentParkingGateAccessor(DeviceTypeDto deviceTypeDto) {
        try {
            return getBeanByDeviceType(deviceTypeDto, AbstractIntelligentParkingGateAccessor.class);
        } catch (Exception e) {
            log.error("getIntelligentParkingGateAccessor", e);
        }
        return null;
    }

    /**
     * 得到智慧门禁的相关能力
     *
     * @param deviceTypeDto
     * @return
     */
    public static AbstractIntelligentEntranceGuardAccessor getIntelligentEntranceGuardAccessor(DeviceTypeDto deviceTypeDto) {
        try {
            return getBeanByDeviceType(deviceTypeDto, AbstractIntelligentEntranceGuardAccessor.class);
        } catch (Exception e) {
            log.error("getIntelligentEntranceGuardAccessor", e);
        }
        return null;
    }

    /**
     * 得到智慧监控的相关能力
     *
     * @param deviceTypeDto
     * @return
     */
    public static AbstractIntelligentMonitorAccessor getIntelligentMonitorAccessor(DeviceTypeDto deviceTypeDto) {
        try {
            return getBeanByDeviceType(deviceTypeDto, AbstractIntelligentMonitorAccessor.class);
        } catch (Exception e) {
            log.error("getIntelligentMonitorAccessor", e);
        }
        return null;
    }

    /**
     * 得到接入应用的基础能力
     *
     * @param deviceTypeDto
     * @return
     */
    public static Accessor getBaseAccessor(DeviceTypeDto deviceTypeDto) {
        try {
            return getBeanByDeviceType(deviceTypeDto, Accessor.class);
        } catch (Exception e) {
            log.error("getBaseAccessor", e);
        }
        return null;
    }


    private static <T extends Accessor> T getBeanByDeviceType(DeviceTypeDto deviceTypeDto, Class<T> cls) throws Exception {
        for (Class<?> typeAccessorClass : typeAccessorClasses) {
            if (typeAccessorClass.getSimpleName().contains(deviceTypeDto.getCode())) {
                return (T) SpringUtils.getBean(typeAccessorClass);
            }
        }
        throw new CheckErrorException("该设备类型没有对接");
    }

}
